<?php
!defined('IN_HDWIKI') && exit('Access Denied');
class synonymmodel {
	var $db;
	var $base;
	function synonymmodel(&$base) {
		$this->base = $base;
		$this->db = $base->db;
	}
	function get_synonym_by_dest($did='',$title){
		$synonymlist=array();
		if(!empty($did)){
			$query=$this->db->query("SELECT * FROM ".DB_TABLEPRE."synonym WHERE destdid=$did ORDER BY id");
		}elseif(!empty($title)){
			$query=$this->db->query("SELECT * FROM ".DB_TABLEPRE."synonym WHERE desttitle='$title' ORDER BY id");
		}else{
			return false;
		}
		while($synonym=$this->db->fetch_array($query)){
			$synonymlist[]=$synonym;
		}
		return $synonymlist;
	}
	
	function get_synonym_by_src($title,$id=''){
		if(!empty($title)){
			return $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."synonym WHERE srctitle='$title'");
		}elseif(is_numeric($id)){
			return $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."synonym WHERE id=$id");
		}else{
			return false;
		}
	}
	
	function savesynonym($destdid,$desttitle,$srctitles){
		$this->removesynonym($destdid);
		$sql="REPLACE INTO ".DB_TABLEPRE."synonym (srctitle,destdid,desttitle) VALUES";
		foreach($srctitles as $srctitle){
			if(!empty($srctitle))
				$sql.="('$srctitle',$destdid,'$desttitle'),";
		}
		$sql=substr($sql,0,-1);
		$this->db->query($sql);
		return $this->db->affected_rows();
	}
	
	function removesynonym($destdid){
		if(is_array($destdid)){
			$dids = implode(',',$destdid);
			$this->db->query("DELETE FROM ".DB_TABLEPRE."synonym WHERE destdid IN ($dids) ");
		}else{
			$this->db->query("DELETE FROM ".DB_TABLEPRE."synonym WHERE destdid=$destdid");
		}
		return $this->db->affected_rows();
	}
	
	function synonym_change_doc($did,$title){
		$this->db->query("UPDATE ".DB_TABLEPRE."synonym SET desttitle='$title' WHERE destdid=$did");
		return $this->db->affected_rows();
	}
	
	function search_synonym_num($cid,$title,$author,$starttime,$endtime){
		$did=array();
		$sql="SELECT  count(*)  FROM ".DB_TABLEPRE."synonym s LEFT JOIN ".DB_TABLEPRE."doc d ON d.did = s.destdid where 1=1 ";
		if($cid){
			$query=$this->db->query("SELECT did FROM ".DB_TABLEPRE."doc  WHERE cid = $cid");
			while($doc=$this->db->fetch_array($query)){
				$did[]=$doc['did'];
			}
			if(is_array($did)){
				$sql=$sql." AND s.destdid IN (".implode(',',$did).") ";
			}
		}
		if($title){
			$synonym=$this->db->fetch_first("SELECT destdid FROM ".DB_TABLEPRE."synonym WHERE srctitle='$title'");
			if($synonym[destdid]){
				$sql=$sql." AND (s.desttitle ='$title' OR s.destdid =$synonym[destdid]) ";
			}else{
				$sql=$sql." AND s.desttitle ='$title' ";
			}
		}
		if($author){
			$query=$this->db->query("SELECT did FROM ".DB_TABLEPRE."doc WHERE author='$author'");
			while($doc=$this->db->fetch_array($query)){
				$did[]=$doc['did'];
			}
			if(is_array($did)){
				$sql=$sql." AND s.destdid IN (".implode(',',$did).") ";
			}
		}
		if($starttime){
			$sql=$sql." AND d.time>=$starttime ";
		}
		if($endtime){
			$sql=$sql." AND d.time<=$endtime ";
		}
		return $this->db->result_first($sql);
	}
	
	function search_synonym($start=0,$limit=10, $cid='', $title='',$author='', $starttime='', $endtime='' ){
		$doclist=array();
		$i=0;
		$sql="SELECT  s.*,d.title title,d.author author,d.authorid authorid,d.time time FROM ".DB_TABLEPRE."synonym s LEFT JOIN ".DB_TABLEPRE."doc d ON d.did = s.destdid where 1=1 ";
		if($cid){
			$query=$this->db->query("SELECT did FROM ".DB_TABLEPRE."doc  WHERE cid = $cid");
			while($doc=$this->db->fetch_array($query)){
				$did[]=$doc['did'];
			}
			if(is_array($did)){
				$sql=$sql." and s.destdid in (".implode(',',$did).") ";
			}
		}
		if($title){
			$synonym=$this->db->fetch_first("SELECT destdid FROM ".DB_TABLEPRE."synonym WHERE srctitle='$title'");
			if($synonym[destdid]){
				$sql=$sql." AND (s.desttitle ='$title' OR s.destdid =$synonym[destdid]) ";
			}else{
				$sql=$sql." AND s.desttitle ='$title' ";
			}
		}
		if($author){
			$query=$this->db->query("SELECT did FROM ".DB_TABLEPRE."doc  WHERE author='$author'");
			while($doc=$this->db->fetch_array($query)){
				$destdid[]=$doc['did'];
			}
			if(is_array($destdid)){
				$sql=$sql." AND s.destdid IN (".implode(',',$destdid).") ";
			}
		}
		if($starttime){
			$sql=$sql." AND d.time>=$starttime ";
		}
		if($endtime){
			$sql=$sql." AND d.time<=$endtime ";
		}
		$sql=$sql." ORDER BY s.desttitle,s.id LIMIT $start,$limit ";
		$query=$this->db->query($sql);
		while($synonym=$this->db->fetch_array($query)){
			$synonym['time'] = $this->base->date($synonym['time']);
			if($synonymlist[$i-1]['destdid']!==$synonym['destdid']){
				$synonymlist[$i]=$synonym;
				$i++;
			}else{
				$synonymlist[$i-1]['srctitle'].=",".$synonym['srctitle'];
				$synonymlist[$i-1]['did'].=",".$synonym['did'];
			}
		}
		return $synonymlist;
	}
	
	function is_filter($srctitles,$desttitle='',$type=false){
		foreach($srctitles as $srctitle){
			if($srctitle==$desttitle)
				return array(-2,$srctitle);
			if($_ENV['doc']->have_danger_word($srctitle))
				return array(-3,$srctitle);
			if(!empty($desttitle)){
				if($type){
					if($synonym=$this->get_synonym_by_dest('',$desttitle)){
						return array(-4,$desttitle);
					}
				}
				if($synonym=$this->get_synonym_by_src($srctitle)){
					 if(string::haddslashes($synonym['desttitle'],1)!=$desttitle)
						return array(-5,$srctitle,$synonym['desttitle']);
				}
				if($synonym=$this->get_synonym_by_dest('',$srctitle)){
					return array(-6,$srctitle);
				}
			}
		}
		return array(1,'');
	}
	
	function encode_data($datas){
		foreach($datas as $data){
			$return_data.=$data.";";
		}
		return $return_data;
	}
}
?>
